package servlet;

import entity.Emp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @date 2025/9/16
 */
@WebServlet("*.emp")
public class EmpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        String path = req.getServletPath();
        switch (path) {
            case "/add.emp":
                addEmp(req,resp);
                break;
            case "/findAll.emp":
                findAll(req,resp);
                break;
            case "/findById.emp":
                findById(req,resp);
                break;
            case "/modify.emp":
                modifyEmp(req,resp);
                break;
            case "/delete.emp":
                deleteEmp(req,resp);
                break;
            default:
                break;
        }
    }
    protected void addEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String dept_id = req.getParameter("dept_id");
        String join_time = req.getParameter("join_time");
        String salary = req.getParameter("salary");
        String money = req.getParameter("money");
        String address = req.getParameter("address");
        String description = req.getParameter("description");
        String m_id = req.getParameter("m_id");

        Connection conn = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ems?useUnicode=true&characterEncoding=utf-8","root","hdyyst");
            String sql = new StringBuffer()
                    .append(" insert into t_emp  ")
                    .append(" (name,gender,dept_id,join_time,salary,money,address,description,m_id)  ")
                    .append(" values (?,?,?,?,?,?,?,?,?)  ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,gender);
            ps.setString(3,dept_id);
            ps.setString(4,join_time);
            ps.setString(5,salary);
            ps.setString(6,money);
            ps.setString(7,address);
            ps.setString(8,description);
            ps.setString(9,m_id);
            ps.executeUpdate();
            req.getRequestDispatcher("/findAll.emp").forward(req,resp);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Emp> emps = new ArrayList<Emp>();

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ems?useUnicode=true&characterEncoding=utf-8","root","hdyyst");
            String sql = new StringBuffer()
                    .append(" select * ")
                    .append("  from t_emp ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                Emp emp = new Emp();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setGender(rs.getString("gender"));
                emp.setDept_id(rs.getInt("dept_id"));
                emp.setJoin_time(rs.getString("join_time"));
                emp.setSalary(rs.getDouble("salary"));
                emp.setMoney(rs.getDouble("money"));
                emp.setAddress(rs.getString("address"));
                emp.setDescription(rs.getString("description"));
                emp.setM_id(rs.getInt("m_id"));
                emps.add(emp);
            }
            req.setAttribute("emps",emps);
            req.getRequestDispatcher("/empList.jsp").forward(req,resp);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String id = req.getParameter("id");

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Emp emp = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ems?useUnicode=true&characterEncoding=utf-8","root","hdyyst");
            String sql = new StringBuffer()
                    .append(" select *  ")
                    .append("  from t_emp ")
                    .append(" where id = ?  ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                emp = new Emp();
                emp.setId(rs.getInt("id"));
                emp.setName(rs.getString("name"));
                emp.setGender(rs.getString("gender"));
                emp.setDept_id(rs.getInt("dept_id"));
                emp.setJoin_time(rs.getString("join_time"));
                emp.setSalary(rs.getDouble("salary"));
                emp.setMoney(rs.getDouble("money"));
                emp.setAddress(rs.getString("address"));
                emp.setDescription(rs.getString("description"));
                emp.setM_id(rs.getInt("m_id"));
            }
            req.setAttribute("emp",emp);
            req.getRequestDispatcher("/modifyEmp.jsp").forward(req,resp);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    protected void modifyEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String dept_id = req.getParameter("dept_id");
        String join_time = req.getParameter("join_time");
        String salary = req.getParameter("salary");
        String money = req.getParameter("money");
        String address = req.getParameter("address");
        String description = req.getParameter("description");
        String m_id = req.getParameter("m_id");


        Connection conn = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ems?useUnicode=true&characterEncoding=utf-8","root","hdyyst");
            String sql = new StringBuffer()
                    .append(" update t_emp  ")
                    .append("  set ")
                    .append("  name = ? ")
                    .append("  gender = ? ")
                    .append("  dept_id = ? ")
                    .append("  join_time = ? ")
                    .append("  salary = ? ")
                    .append("  money = ? ")
                    .append("  address = ? ")
                    .append("  description = ? ")
                    .append("  m_id = ?  ")
                    .append(" where id = ?  ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            ps.setString(2,gender);
            ps.setString(3,dept_id);
            ps.setString(4,join_time);
            ps.setString(5,salary);
            ps.setString(6,money);
            ps.setString(7,address);
            ps.setString(8,description);
            ps.setString(9,m_id);
            ps.setString(10, id);
            ps.executeUpdate();
            resp.sendRedirect(req.getContextPath()+"/findAll.emp");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    protected void deleteEmp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String id = req.getParameter("id");

        Connection conn = null;
        PreparedStatement ps = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/ems?useUnicode=true&characterEncoding=utf-8","root","hdyyst");
            String sql = new StringBuffer()
                    .append(" delete from t_emp ")
                    .append("  where id = ?  ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,id);
            ps.executeUpdate();
            resp.sendRedirect(req.getContextPath()+"/findAll.emp");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
